package com.example.myapplication;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;

public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ScrollView scrollView = new ScrollView(getContext());


        LinearLayout rootLayout = new LinearLayout(getContext());
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        rootLayout.setPadding(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16));


        scrollView.addView(rootLayout);


        addNewsItem(rootLayout,
                "Android 14正式发布",
                "2023-10-05",
                "谷歌今日正式发布了Android 14系统，新系统在隐私安全、用户体验和性能优化方面进行了多项改进...",
                R.drawable.android14);


        addDivider(rootLayout);


        addNewsItem(rootLayout,
                "全球股市十月开门红",
                "2023-10-04",
                "受美联储暂停加息预期影响，全球主要股市在十月份首个交易日普遍上涨，其中科技股表现最为亮眼...",
                R.drawable.gushi);


        addDivider(rootLayout);


        addNewsItem(rootLayout,
                "中国女排亚运会夺冠",
                "2023-10-03",
                "在昨晚结束的亚运会女排决赛中，中国女排以3:0战胜日本队，成功卫冕亚运会冠军...",
                R.drawable.duoguan);

        return scrollView;  // 返回包含滚动功能的ScrollView
    }

    private void addNewsItem(LinearLayout parent, String title, String date, String content, int imageResId) {

        LinearLayout newsItem = new LinearLayout(getContext());
        newsItem.setOrientation(LinearLayout.VERTICAL);
        newsItem.setPadding(0, dpToPx(8), 0, dpToPx(16));


        TextView titleView = new TextView(getContext());
        titleView.setText(title);
        titleView.setTextSize(18);
        titleView.setPadding(0, 0, 0, dpToPx(8));
        newsItem.addView(titleView);


        TextView contentView = new TextView(getContext());
        contentView.setText(content);
        contentView.setTextSize(14);
        contentView.setPadding(0, 0, 0, dpToPx(12));
        newsItem.addView(contentView);


        ImageView imageView = new ImageView(getContext());
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                dpToPx(200));  // 设置图片高度为200dp
        imageParams.setMargins(0, dpToPx(8), 0, 0);
        imageView.setLayoutParams(imageParams);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(imageResId);
        newsItem.addView(imageView);


        TextView dateView = new TextView(getContext());
        dateView.setText(date);
        dateView.setTextSize(12);
        dateView.setTextColor(0xFF888888);
        dateView.setGravity(Gravity.END);
        dateView.setPadding(0, dpToPx(8), 0, 0);
        newsItem.addView(dateView);

        parent.addView(newsItem);
    }

    private void addDivider(LinearLayout parent) {
        View divider = new View(getContext());
        divider.setBackgroundColor(0xFFCCCCCC);
        parent.addView(divider, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dpToPx(1)  // 1像素高的分隔线
        ));
    }


    private int dpToPx(int dp) {
        float density = getResources().getDisplayMetrics().density;
        return Math.round(dp * density);
    }
}